import { Injectable } from '@angular/core';

export class Storage {
  expire?: string;
  data: any;
}

@Injectable()
export class LocalstoreService {

  constructor() { }

  private supportLoaclStorage(): boolean {
      if (window.localStorage) {
          return true;
      }
      return false;
  }

  private setLocalStorageItem(name: string, value: any, expire: string) {
    let data: Storage = { data: value };
    if(expire) {
      data.expire = expire;
    }
    window.localStorage.setItem(name, JSON.stringify(data));
  }

  private getLocalStorageItem(name: string): any {
    let storageItem = window.localStorage.getItem(name);
    let data = null;
    if(storageItem) {
      let item = JSON.parse(storageItem);
      if(item.expire) {
        if(new Date() > new Date(item.expire)) {
          data = null;
          this.removeItem(name);
        } else {
          data = item.data;
        }
      } else {
        data = item.data;
      }
    }
    return data;
  }

  private setCookieItem(name: string, value: any, expire: string) {
    let date = null;
    let data: Storage = { data: value };
    if(expire) {
      date = new Date(expire);
      data.expire = date;      
    } else {
      date = new Date();
      date.setTime(date.getTime() - 1000);
    }
    document.cookie = name + "=" + JSON.stringify(data) + ";expires=" + date.toUTCString() + ";path=/";
  }

  private getCookieItem(name: string): any {
    let cookieItem = null; 
    let data = null;
    let startIndex = null;
    let endIndex = null;
    if(document.cookie.length > 0) {
      startIndex = document.cookie.indexOf(name + "=");
      if(startIndex != -1) {
        endIndex = document.cookie.indexOf(';', startIndex + name.length + 1);
        if(endIndex === -1) endIndex = document.cookie.length;
        cookieItem = JSON.parse(document.cookie.substring(startIndex + name.length + 1, endIndex));
        if(cookieItem.expire) {
          if(new Date() > new Date(cookieItem.expire)) {
            data = null;
            this.removeItem(name);
          } else {
            data = cookieItem.data;
          }
        } else {
          data = cookieItem.data;
        }
      }
    } 
    return data;
  }


  setItem(name: string, value: any, expire: string) {
    if(this.supportLoaclStorage()) {
      this.setLocalStorageItem(name, value, expire);
    } else {
      this.setCookieItem(name, value, expire);
    }
  }

  getItem(name: string): any | null {
    let item = null;
    if(this.supportLoaclStorage()) {
      item = this.getLocalStorageItem(name);
    } else {
      item = this.getCookieItem(name);
    }
    return item;
  }

  removeItem(name: string) {
    if(this.supportLoaclStorage()) {
      window.localStorage.removeItem(name);
    } else {
      let date = new Date();
      date.setSeconds(date.getSeconds() - 1);
      this.setCookieItem(name, '', date.toUTCString());
    }
  }

  clear() {
    let date = new Date();
    date.setSeconds(date.getSeconds() - 1);
    if(this.supportLoaclStorage()) {
      window.localStorage.clear();
    } else {
      if(document.cookie.length > 0) {
        let cookieArray = document.cookie.split('; ');
        let cookieItem = null;
        for(cookieItem in cookieArray) {
          let item = cookieItem.split('=');
          this.setCookieItem(item[0], '', date.toUTCString());
        }
      }
    }
  }

}
